package com.xb.loan.app.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qicai.common.exception.ServiceDataException;
import com.qicai.common.page.Page;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import org.springframework.beans.BeanUtils;
import com.xb.loan.app.mapper.TLoanPlatformTypeMapper;
import com.xb.loan.app.service.TLoanPlatformTypeService;
import com.xb.loan.app.model.TLoanPlatformType;
import com.xb.loan.app.dto.TLoanPlatformTypeDTO;
import java.util.ArrayList;
import java.util.List;
/**
 * 贷款平台特征等信息配置业务层访问接口实现
 * @author  wuql
 * @version tLoanPlatformtypeServiceImpl.java, v 0.1  2017-11-6 15:10:01
 */
@Service("tLoanPlatformtypeService")
public class TLoanPlatformTypeServiceImpl implements TLoanPlatformTypeService {

private static final org.slf4j.Logger logger = LoggerFactory.getLogger(TLoanPlatformTypeServiceImpl.class);
    /**贷款平台特征等信息配置数据层访问接口实现**/
	@Autowired
	private TLoanPlatformTypeMapper tLoanPlatformtypeMapper;

    @Override
       public Long saveTLoanPlatformType(TLoanPlatformTypeDTO tLoanPlatformtypeDTO) {
           logger.info("saveTLoanPlatformType tLoanPlatformtypeDTO:{}", tLoanPlatformtypeDTO);
                  long rows = 0;
                  try {
                      if (tLoanPlatformtypeDTO != null) {
                          TLoanPlatformType tLoanPlatformtype = new TLoanPlatformType();
                          BeanUtils.copyProperties(tLoanPlatformtypeDTO, tLoanPlatformtype);
                          if (tLoanPlatformtype.getId() != null && tLoanPlatformtype.getId() != 0) {
                              //更新
                              tLoanPlatformtype.setUpdateTime(System.currentTimeMillis());
                              rows = tLoanPlatformtypeMapper.updateByPrimaryKeySelective(tLoanPlatformtype);
                          } else {
                              //插入
                              tLoanPlatformtype.setCreateTime(System.currentTimeMillis());
                              rows = tLoanPlatformtypeMapper.insert(tLoanPlatformtype);
                          }
                      }
                  } catch (Exception e) {
                      logger.error(e.getMessage(), e);
                      throw new ServiceDataException("saveTLoanPlatformType Error::", e);
                  }
                  return rows;
       }

       @Override
       public Long deleteTLoanPlatformType(Long id) {
       logger.info("deleteTLoanPlatformType tLoanPlatformtypeId:{}", id);
           long rows = 0;
           try {
               rows = tLoanPlatformtypeMapper.deleteByPrimaryKey(id);
           } catch (Exception e) {
               logger.error(e.getMessage(), e);
               throw new ServiceDataException("deletetLoanPlatformtype Error::", e);
           }
           return rows;
       }

       @Override
       public TLoanPlatformTypeDTO queryTLoanPlatformTypeById(Long id) {
       logger.info("queryTLoanPlatformTypeById tLoanPlatformtypeId:{}", id);
           TLoanPlatformTypeDTO tLoanPlatformtypeDTO = null;
           try {
              TLoanPlatformType tLoanPlatformtype = tLoanPlatformtypeMapper.selectByPrimaryKey(id);
                           if(tLoanPlatformtype!=null){
                           tLoanPlatformtypeDTO = new TLoanPlatformTypeDTO();
                               BeanUtils.copyProperties(tLoanPlatformtype,tLoanPlatformtypeDTO);
                           }
           } catch (Exception e) {
               logger.error(e.getMessage(), e);
               throw new ServiceDataException("querytLoanPlatformtypeById", e);
           }
           return tLoanPlatformtypeDTO;
       }

       @Override
       public List<TLoanPlatformTypeDTO> queryTLoanPlatformType(TLoanPlatformTypeDTO tLoanPlatformtypeDTO) {
            logger.info("queryTLoanPlatformType tLoanPlatformtypeDTO:{}", tLoanPlatformtypeDTO);
                  List<TLoanPlatformTypeDTO> list = null;
                  List<TLoanPlatformType> tLoanPlatformtypeList = null;
                  try {
                      if (tLoanPlatformtypeDTO != null) {
                          TLoanPlatformType tLoanPlatformtype = new TLoanPlatformType();
                          BeanUtils.copyProperties(tLoanPlatformtypeDTO, tLoanPlatformtype);
                          tLoanPlatformtypeList = tLoanPlatformtypeMapper.select(tLoanPlatformtype);
                      } else {
                          Example example = new Example(TLoanPlatformType.class);
                          // 排序规则
                          example.orderBy("id").desc();
                          tLoanPlatformtypeList = tLoanPlatformtypeMapper.selectByExample(example);
                      }
                      if (tLoanPlatformtypeList != null && tLoanPlatformtypeList.size() > 0) {
                          list = convertTLoanPlatformTypeDTO(tLoanPlatformtypeList);
                      }

                  } catch (Exception e) {
                      logger.error(e.getMessage(), e);
                  }
                  return list;
       }

       @Override
       public Page<TLoanPlatformTypeDTO> queryTLoanPlatformTypePage(TLoanPlatformTypeDTO tLoanPlatformtypeDTO) {
        logger.info("queryTLoanPlatformTypePage tLoanPlatformtypeDTO:{}", tLoanPlatformtypeDTO);
           Page<TLoanPlatformTypeDTO> page = null;
           Integer currentPage = tLoanPlatformtypeDTO.getPage();
           if (currentPage < 1) currentPage = 1;
           try {
               page = new Page<TLoanPlatformTypeDTO>(currentPage, tLoanPlatformtypeDTO.getPageCount());
               PageHelper.startPage(currentPage, tLoanPlatformtypeDTO.getPageCount());
               Example example = new Example(TLoanPlatformType.class);
               // 排序规则
               example.orderBy("id").desc();
               List<TLoanPlatformType> list = tLoanPlatformtypeMapper.selectByExample(example);

               if (list != null && list.size() > 0) {
                   PageInfo pageInfo = new PageInfo(list);
                   page.setTotalCount(Long.valueOf(pageInfo.getTotal()).intValue());
                    List<TLoanPlatformTypeDTO> tLoanPlatformtypeDTOList = convertTLoanPlatformTypeDTO(list);
                   page.setResult(tLoanPlatformtypeDTOList);
               }
           } catch (Exception e) {
               logger.error(e.getMessage(), e);
           }

           return page;
       }
	  private List<TLoanPlatformTypeDTO> convertTLoanPlatformTypeDTO(List<TLoanPlatformType> tLoanPlatformtypeList) {
            
            if (tLoanPlatformtypeList == null) return null;
             List<TLoanPlatformTypeDTO> tLoanPlatformtypeDTOs = new ArrayList<>();
             for (TLoanPlatformType tLoanPlatformtype : tLoanPlatformtypeList
                     ) {
                 TLoanPlatformTypeDTO tLoanPlatformtypeDTO = new TLoanPlatformTypeDTO();
                 BeanUtils.copyProperties(tLoanPlatformtype, tLoanPlatformtypeDTO);
                 tLoanPlatformtypeDTOs.add(tLoanPlatformtypeDTO);
             }
             return tLoanPlatformtypeDTOs;
         }
}